package com.jin10.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jin10.bean.EconomicsResponse;
import com.jin10.constant.TitleHandlerClassMap;
import com.jin10.dto.NonFarmArrangeDto;
import com.jin10.entity.PublishCalendar;
import com.jin10.schedule.MonitorCalendar;
import com.jin10.service.IArticleArrangeService;
import com.jin10.service.IPublishCalendarService;
import com.jin10.service.ITitleHandlerService;
import com.jin10.vo.NonFarmArticleArrangeVo;
import com.jin10.vo.NonFarmBaseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author raofy
 * @date 2022-03-09 11:05
 * @desc
 */
@Service
public class ArticleArrangeServiceImpl implements IArticleArrangeService {
    private static final Logger log = LoggerFactory.getLogger(MonitorCalendar.class);
    private static final String URL = "https://cdn-rili.jin10.com/web_data/%s/daily/%s/%s/economics.json";

    @Autowired
    TitleHandlerClassMap titleHandlerClassMap;

    @Autowired
    IPublishCalendarService iPublishCalendarService;

    @Override
    public NonFarmArticleArrangeVo getNonFarmArrange(NonFarmArrangeDto dto) {
        NonFarmArticleArrangeVo nonFarmArticleArrangeVo = new NonFarmArticleArrangeVo();
        PublishCalendar one = iPublishCalendarService.getOne(new LambdaQueryWrapper<PublishCalendar>().eq(PublishCalendar::getPublished, 1).orderByDesc(PublishCalendar::getCreateTime).last("limit 1"));
        if (Objects.nonNull(one)) {

            int year = one.getReleaseTimeYear();
            int month = one.getReleaseTimeMonth();
            int day = one.getReleaseTimeDay();

            String url = String.format(URL, year, getMonth(month), getDay(day));
            String body = HttpRequest.get(url).execute().body();
            List<EconomicsResponse> response = JSON.parseArray(body, EconomicsResponse.class);
            StringBuffer stringBuffer = new StringBuffer();
            if (!response.isEmpty()) {
                for (EconomicsResponse economicsResponse : response) {
                    if (Objects.nonNull(economicsResponse.getIndicatorId())) {
                        if (economicsResponse.getIndicatorId() == 75 || economicsResponse.getIndicatorId() == 76) {

                            ITitleHandlerService titleHandler = (ITitleHandlerService) titleHandlerClassMap.getTitleHandler(economicsResponse.getIndicatorId());
                            if (Objects.nonNull(titleHandler)) {
                                if (economicsResponse.getIndicatorId() == 75) {
                                    if (Objects.nonNull(dto.getActual())) {
                                        economicsResponse.setActual(dto.getActual());
                                    }
                                    if (Objects.nonNull(dto.getConsensus())) {
                                        economicsResponse.setConsensus(dto.getConsensus());
                                    }
                                    if (Objects.nonNull(dto.getPrevious())) {
                                        economicsResponse.setPrevious(dto.getPrevious().toString());
                                    }
                                    nonFarmArticleArrangeVo = (NonFarmArticleArrangeVo) titleHandler.doAction(economicsResponse, year, getMonth(month), getDay(day), dto);
                                    stringBuffer.append(nonFarmArticleArrangeVo.getData());
                                } else {
                                    stringBuffer.append((String) titleHandler.doAction(economicsResponse, year, getMonth(month), getDay(day), dto));
                                }
                            }
                        }
                    }
                }
                nonFarmArticleArrangeVo.setData(stringBuffer.toString());
                return nonFarmArticleArrangeVo;
            }
        }
        return nonFarmArticleArrangeVo;
    }

    @Override
    public NonFarmBaseVo getTitle(NonFarmArrangeDto dto) {

        NonFarmBaseVo nonFarmArticleArrangeVo = new NonFarmBaseVo();
        PublishCalendar one = iPublishCalendarService.getOne(new LambdaQueryWrapper<PublishCalendar>().eq(PublishCalendar::getPublished, 1).orderByDesc(PublishCalendar::getCreateTime).last("limit 1"));
        if (Objects.nonNull(one)) {

            int year = one.getReleaseTimeYear();
            int month = one.getReleaseTimeMonth();
            int day = one.getReleaseTimeDay();

            String url = String.format(URL, year, getMonth(month), getDay(day));
            String body = HttpRequest.get(url).execute().body();
            List<EconomicsResponse> response = JSON.parseArray(body, EconomicsResponse.class);
            StringBuffer stringBuffer = new StringBuffer();
            if (!response.isEmpty()) {
                for (EconomicsResponse economicsResponse : response) {
                    if (Objects.nonNull(economicsResponse.getIndicatorId())) {
                        if (economicsResponse.getIndicatorId() == 75 || economicsResponse.getIndicatorId() == 76) {

                            ITitleHandlerService titleHandler = (ITitleHandlerService) titleHandlerClassMap.getTitleHandler(economicsResponse.getIndicatorId());
                            if (Objects.nonNull(titleHandler)) {
                                if (economicsResponse.getIndicatorId() == 75) {
                                    if (Objects.nonNull(dto.getActual())) {
                                        economicsResponse.setActual(dto.getActual());
                                    }
                                    if (Objects.nonNull(dto.getConsensus())) {
                                        economicsResponse.setConsensus(dto.getConsensus());
                                    }
                                    if (Objects.nonNull(dto.getPrevious())) {
                                        economicsResponse.setPrevious(dto.getPrevious().toString());
                                    }
                                    nonFarmArticleArrangeVo.setTitle(String.format("%s-%s非农数据：前值%s，预期%s，公布值%s",Integer.valueOf(month) == 1 ? year - 1 : year, economicsResponse.getTimePeriod(), economicsResponse.getPrevious(), economicsResponse.getConsensus(), economicsResponse.getActual() ));
                                    nonFarmArticleArrangeVo.setActual(Float.valueOf(economicsResponse.getActual().toString()));
                                    nonFarmArticleArrangeVo.setConsensus(Float.valueOf(economicsResponse.getConsensus().toString()));
                                    nonFarmArticleArrangeVo.setPrevious(Float.valueOf(economicsResponse.getPrevious()));
                                }
                            }
                        }
                    }
                }
                return nonFarmArticleArrangeVo;
            }
        }
        return nonFarmArticleArrangeVo;
    }


    public String getMonth(int month) {
        String[] months = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        return months[month];
    }

    public String getDay(int day) {
        String[] days = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13",
                "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
        return days[day];
    }
}
